//
//  LKSearchBar.m
//  YLHelper
//
//  Created by she1323 on 2018/4/16.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKSearchBar.h"
#import "UIImage+Tool.h"

@interface LKSearchBar()<UITextFieldDelegate>

@property (nonatomic, assign) CGFloat placeholderWidth;

@end


@implementation LKSearchBar
// placeholder 和icon 和 间隙的整体宽度


// icon宽度
static CGFloat const searchIconW = 10.0;
// icon与placeholder间距
static CGFloat const iconSpacing = 10.0;
// 占位文字的字体大小
static CGFloat const placeHolderFont = 13.0;

- (void)layoutSubviews {
    [super layoutSubviews];
//    // 设置背景图片
//    UIImage *backImage = [UIImage imageWithColor:[UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1]];
//    [self setBackgroundImage:backImage];

    self.backgroundImage = [self imageWithColor:[UIColor clearColor] size:self.bounds.size];
//    self.backgroundColor = COLOR_HEX(0xEDF2F6);
    
//    self.backgroundColor = [UIColor redColor];
    for (UIView *view in [self.subviews lastObject].subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            
            UIButton *cancelBtn = (UIButton *)view;
            
            cancelBtn.enabled = YES;
            
            [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
            cancelBtn.titleLabel.font = [UIFont systemFontOfSize:14];
            [cancelBtn setTitleColor:blueDefault forState:UIControlStateNormal];
//            cancelBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 20, 0, -20);
            cancelBtn.backgroundColor = [UIColor whiteColor];
//            cancelBtn.size = CGSizeMake(100, 100);
//            [cancelBtn addTarget:self action:@selector(cancelButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
            
        }
        
        
        
        
        //输入框的设置
        if ([view isKindOfClass:[UITextField class]]) {
            UITextField *field = (UITextField *)view;
            // 重设field的frame
            field.frame = CGRectMake(15.0, 0, self.frame.size.width-30.0, self.frame.size.height);
            field.backgroundColor = COLOR_HEX(0xEDF2F6);
            field.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];

            field.borderStyle = UITextBorderStyleNone;
            field.layer.cornerRadius = 2.0f;
            field.layer.masksToBounds = YES;
            field.clearButtonMode = UITextFieldViewModeNever;
            // 设置占位文字字体颜色
            [field setValue:[UIColor colorWithRed:156/255.0 green:156/255.0 blue:156/255.0 alpha:1] forKeyPath:@"_placeholderLabel.textColor"];
            [field setValue:[UIFont systemFontOfSize:placeHolderFont] forKeyPath:@"_placeholderLabel.font"];

            if (@available(iOS 11.0, *)) {
                // 先默认居中placeholder
//                [self setPositionAdjustment:UIOffsetMake((field.frame.size.width-self.placeholderWidth)/2, 0) forSearchBarIcon:UISearchBarIconSearch];
            }
        }
    }
}
//
// 开始编辑的时候重置为靠左
//- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
//    // 继续传递代理方法
//    if ([self.delegate respondsToSelector:@selector(searchBarShouldBeginEditing:)]) {
//        [self.delegate searchBarShouldBeginEditing:self];
//    }
//    if (@available(iOS 11.0, *)) {
//        [self setPositionAdjustment:UIOffsetZero forSearchBarIcon:UISearchBarIconSearch];
//    }
//    return YES;
//}

// 结束编辑的时候设置为居中
//- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
//    if ([self.delegate respondsToSelector:@selector(searchBarShouldEndEditing:)]) {
//        [self.delegate searchBarShouldEndEditing:self];
//    }
//    if (@available(iOS 11.0, *)) {
//        [self setPositionAdjustment:UIOffsetMake((textField.frame.size.width - self.placeholderWidth)/2, 0) forSearchBarIcon:UISearchBarIconSearch];
//    }
//    return YES;
//}
//
// 计算placeholder、icon、icon和placeholder间距的总宽度
//- (CGFloat)placeholderWidth {
//    if (!_placeholderWidth) {
//        CGSize size = [self.placeholder boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:placeHolderFont]} context:nil].size;
//        _placeholderWidth = size.width + iconSpacing + searchIconW;
//    }
//    return _placeholderWidth;
//}
//
/** 取消searchBar背景色 */
- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
